package org.angmarch.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by meijun on 17-4-1.
 */

public class FlowPathView extends View {

    private Paint mPaint;
    private Paint mDotPaint;

    public FlowPathView(Context context) {
        super(context);

        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.parseColor("#FF018877"));
        mPaint.setStrokeWidth(4);

        mDotPaint = new Paint();
        mDotPaint.setAntiAlias(true);
        mDotPaint.setStyle(Paint.Style.FILL);
        mDotPaint.setColor(Color.parseColor("#FF018877"));
        mDotPaint.setStrokeWidth(4);

    }

    public FlowPathView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FlowPathView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);


    }


    private int mHeight;
    private int mWidth;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        this.mHeight = h;
        this.mWidth = w;
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private int measureHeight(int heightMeasureSpec) {

        int result;
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = Dp2Px(this.getContext(), DEFAULT_HEIGHT);
            if (mode == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);

            }


        }
        return result;


    }




    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //先划线


        if (mIsHalfDisplay==3) {

            canvas.drawLine(mWidth * 1.0f / 2, 0, mWidth * 1.0f / 2, mHeight, mPaint);
        } else if(mIsHalfDisplay==1){
            canvas.drawLine(mWidth * 1.0f / 2, 0, mWidth * 1.0f / 2, mHeight * 1.0f / 2, mPaint);

        }else if(mIsHalfDisplay==2){
            //显示下半部
            canvas.drawLine(mWidth * 1.0f / 2, mHeight *1.0f / 2, mWidth * 1.0f / 2, mHeight, mPaint);


        }


        //画圆


        canvas.drawCircle(mWidth *1.0f / 2,mHeight*1.0f/ 2,Dp2Px(this.getContext(),5),mDotPaint);

    }

    private int  mIsHalfDisplay =  3 ;

    /**
     * 1 显示上半部
     * 2显示下半部
     * 3 全部显示
     * @param isHalfDisplay
     */
    public void setHalfDisplay(int isHalfDisplay) {
        this.mIsHalfDisplay = isHalfDisplay;
        invalidate();
    }


    private static int DEFAULT_WIDHT = 5;
    private static int DEFAULT_HEIGHT = 10;

    private int measureWidth(int widthMeasureSpec) {
        int result;
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = Dp2Px(this.getContext(), DEFAULT_WIDHT);
            if (mode == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);

            }


        }
        return result;


    }

    public void setDotColor(int color){
        mDotPaint.setColor(color);
        invalidate();
    }

    public int Dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public int Px2Dp(Context context, float px) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }
}
